import {_decorator, Button, Component, director, instantiate, Label, Layout, Prefab, UITransform, Node} from 'cc';
import {FishFryDetail} from "db://assets/proto/shared/v1/wuni";
import {TalentHeadComp} from "db://assets/script/game/mainui/talent-head-comp";
import {configsHelper} from "db://assets/script/configs-helper";
import {NormalBuilding} from "db://assets/script/game/city/normal-building";
import {
    UserFishFryListCompleteEvent
} from "db://assets/script/common/events/fish-events/user-fish-fry-list-complete-event";
import Long from "long";
import {GameEvent} from "db://assets/script/game-event";
import {UserFishFryListEvent} from "db://assets/script/common/events/fish-events/user-fish-fry-list-event";
import {userInfo} from "db://assets/script/user-info";
import {lobby} from "db://assets/script/lobby";
import {FishFryInfoComp} from "db://assets/script/game/fish/fish-fry-info-comp";

const {ccclass, property} = _decorator;

@ccclass('DormitoryPlane')
export class DormitoryPlane extends Component {

    @property(Layout)
    fishBox!: Layout

    @property(Label)
    title!: Label

    @property(Button)
    upgradeBtn!: Button

    @property(Prefab)
    fishFryHeadPrefab!: Prefab;

    fishFries: FishFryDetail[] = []

    buid: Long | null = null

    init(building: NormalBuilding) {
        this.buid = building.buildingItem!.buildingUniqueId
        const aac = configsHelper.loadBuildingAssetConfig(building.buildingId)
        if (aac) this.title.string = aac.name
        this.upgradeBtn.node.active = building.upgradeButton.node.active
        this.upgradeBtn.node.on(Button.EventType.CLICK, building.upgrade, building)
        const ffd = userInfo.fishInfo.loadDormitoryFishFries(this.buid)
        if (ffd.length > 0) {
            this.refreshFishFry(new UserFishFryListCompleteEvent(this.buid, ffd))
        } else {
            director.emit(GameEvent.userFishFryRequestList, new UserFishFryListEvent(this.buid))
        }
        const boxUt = this.fishBox.getComponent(UITransform)!
        console.log('boxUt', boxUt)
        this.onEvent()
    }

    onEvent() {
        director.on(GameEvent.userFishFryListResult, this.refreshFishFry, this)
    }

    offEvent() {
        director.off(GameEvent.userFishFryListResult, this.refreshFishFry, this)
    }

    refreshFishFry(event: UserFishFryListCompleteEvent) {
        if (event.buid.neq(this.buid!)) {
            return
        }
        this.fishFries = event.fishFries;
        for (let ff of this.fishFries) {
            const hn = instantiate(this.fishFryHeadPrefab)
            hn.getComponent(TalentHeadComp)!.setHead(ff.fishId, ff.gender, ff.talent, userInfo.loadUid())
            hn.getComponent(UITransform)!.width = 90
            hn.getComponent(UITransform)!.height = 90
            this.fishBox.node.addChild(hn)
            hn.on(Node.EventType.TOUCH_END, () => this._seeFishFry(ff))
        }
    }

    _seeFishFry(fishFry: FishFryDetail) {
        const fryInfo = lobby.instance.addUIWithClickMask('prefab/fish-fry-info', 128, (node) => node.destroy())
        fryInfo.getComponent(FishFryInfoComp)!.init(fishFry!)
    }


    protected onDestroy() {
        this.offEvent()
    }
}